﻿using System;
using System.Collections.Generic;
using System.ServiceProcess;
using System.Text;
using System.Linq;
using Icst.Leaflike.Diagnotise;
using Icst.Leaflike.Util;

namespace Icst.Leaflike.Services
{
    //lfs /n /s Gallery

    //lfs.exe /i /s Gallery "Isct Leaflike Gallery Service" "Isct Leaflike Gallery Service"
    //lfs.exe /i /s Indexing "Isct Leaflike Indexing Service" "Isct Leaflike Indexing Service"
    //lfs.exe /i /s Searching "Isct Leaflike Searching Service" "Isct Leaflike Searching Service"

    //lfs.exe /u /s Gallery
    //lfs.exe /u /s Indexing
    //lfs.exe /u /s Searching

    static class Program
    {
        enum CommandRun { Install, UnInstall, WithoutSerivce, RunService}
        class CommandOptions
        {
            public CommandOptions(string[] args)
            {
                CommandRun = CommandRun.RunService;
                if (args.Contains("/n")) CommandRun = CommandRun.WithoutSerivce;
                if (args.Contains("/i")) CommandRun = CommandRun.Install;
                if (args.Contains("/u")) CommandRun = CommandRun.UnInstall;
                int index = Array.IndexOf(args,"/s");
                if (index != -1)
                {
                    ServiceName = ServiceDisplayName = ServiceDescription = args[index + 1];
                    if (index + 2 < args.Length)
                        ServiceDisplayName = ServiceDescription = args[index + 2];
                    if (index + 3 < args.Length)
                        ServiceDescription = args[index + 3];
                }
            }
            public CommandRun CommandRun { get; private set; }
            public string ServiceName { get; private set; }
            public string ServiceDisplayName { get; private set; }
            public string ServiceDescription { get; private set; }
            public string ServiceRunArgs { get { return "/s " + ServiceName; } }
            public ServiceBase CreateService()
            {
                ServiceBase svc = null;
                switch (ServiceName)
                {
                    case "Isct$Searching": svc = new SearchingService(); break;
                    default: break;
                }
                return svc;
            }
        }
        static void Main()
        {
            CommandOptions options = new CommandOptions(Environment.GetCommandLineArgs());
            switch (options.CommandRun)
            {
                case CommandRun.Install: ServiceManager.Install(options.ServiceName, options.ServiceDisplayName, options.ServiceDescription, options.ServiceRunArgs); break;
                case CommandRun.UnInstall: ServiceManager.UnInstall(options.ServiceName); break;
                case CommandRun.RunService: ServiceBase.Run(new ServiceBase[] { options.CreateService()}); break;
                case CommandRun.WithoutSerivce:
                    {
                        Console.WriteLine("START {0}", options.ServiceName); 
                        (options.CreateService() as IDebugService).DebugStart();
                        Console.ReadLine();
                        break;
                    }
            }
        }
    }
}
